﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 九宫
/// </summary>
public class Palace : MonoBehaviour
{
    int[,] sp = new int[3, 3];//中间的宫
    int[,] array = new int[9, 9];//九宫格数组
    int index = 0;//赋值索引

    private void Start()
    {
        GameObject game = transform.GetChild(0).gameObject;
        while (transform.childCount < 81)
        {
            GameObject clone = Instantiate(game, transform);
            clone.name = game.name;
        }

        InitStartPalace();

        CrossChange();

        FillCorners();

        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++, index++)
            {
                transform.GetChild(index).GetComponent<InputField>().text = array[i, j].ToString();
                if ((((i >= 0 && i <= 2) || (i >= 6 && i <= 8)) && (j >= 3 && j <= 5)) || (((j >= 0 && j <= 2) || (j >= 6 && j <= 8)) && (i >= 3 && i <= 5)))
                {
                    transform.GetChild(index).GetComponent<Image>().color = new Color(0.7f, 0.7f, 0.7f);//中间十字换色
                }
            }
        }
    }
    /// <summary>
    /// 中间的宫
    /// </summary>
    void InitStartPalace()
    {
        int k = 1, temp = 0, m = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                sp[i, j] = k++;
            }
        }

        for (int i = 0; i < 3; i++)
        {
            temp = Random.Range(0, 9);//随机交换
            m = sp[i / 3, i % 3];
            sp[i / 3, i % 3] = sp[temp / 3, temp % 3];
            sp[temp / 3, temp % 3] = m;
        }
    }
    /// <summary>
    /// 十字变换
    /// </summary>
    void CrossChange()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                array[i + 3, j + 3] = sp[i, j];//中间1
                if (i == 0)
                {
                    array[i + 4, j] = sp[i, j];//左2  array[4][0-2]
                    array[i + 5, j + 6] = sp[i, j];//右3  array[5][6-8]
                }
                else if (i == 1)
                {
                    array[i + 4, j] = sp[i, j];//左3  array[5][0-2]
                    array[i + 2, j + 6] = sp[i, j];//右1  array[3][6-8]
                }
                else
                {
                    array[i + 1, j] = sp[i, j];//左1  array[3][0-2]
                    array[i + 2, j + 6] = sp[i, j];//右2  array[4][6-8]
                }

                if (j == 0)
                {
                    array[i, j + 4] = sp[i, j];//上2 array[0-2][4]
                    array[i + 6, j + 5] = sp[i, j];//下3 array[6-8][5]
                }
                else if (j == 1)
                {
                    array[i, j + 4] = sp[i, j];//上3  array[0-2][5]
                    array[i + 6, j + 2] = sp[i, j];//下1  array[6-8][3]
                }
                else
                {
                    array[i, j + 1] = sp[i, j];//上1  array[0-2][4]
                    array[i + 6, j + 2] = sp[i, j];//下2  array[6-8][4]
                }
            }
        }
    }
    /// <summary>
    /// 填充四角
    /// </summary>
    void FillCorners()
    {
        for (int i = 3; i < 6; i++)
        {//上两角
            for (int j = 0; j < 3; j++)
            {
                if (j == 0)
                {
                    array[i - 3, j + 1] = array[i, j];// array[0-2][1]
                    array[i + 3, j + 2] = array[i, j];// array[6-8][2]
                }
                else if (j == 1)
                {
                    array[i - 3, j + 1] = array[i, j];// array[0-2][2]
                    array[i + 3, j - 1] = array[i, j];// array[6-8][0]
                }
                else
                {
                    array[i - 3, j - 2] = array[i, j];// array[0-2][0]
                    array[i + 3, j - 1] = array[i, j];// array[6-8][1]
                }
            }
        }

        for (int i = 3; i < 6; i++)
        {
            for (int j = 6; j < 9; j++)
            {
                if (j == 6)
                {
                    array[i - 3, j + 1] = array[i, j];// array[0-2][7]
                    array[i + 3, j + 2] = array[i, j];// array[6-8][8]
                }
                else if (j == 7)
                {
                    array[i - 3, j + 1] = array[i, j];// array[0-2][8]
                    array[i + 3, j - 1] = array[i, j];// array[6-8][6]
                }
                else
                {
                    array[i - 3, j - 2] = array[i, j];// array[0-2][6]
                    array[i + 3, j - 1] = array[i, j];// array[6-8][7]
                }
            }
        }
    }
}
